#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QGridLayout>
#define _POINT_COUNT 50000
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(QString("QCustomPlot --老人与海 %1").arg(STR_VERSION));

    ui->statusbar->hide();
    fun_intUi(ui->subCurve);
    ui->subCurve->yAxis->setRange(-50,150);

    QSharedPointer<QCPAxisTicker> ValTicker(new QCPAxisTicker);
    ValTicker->setTickCount(5);
    ui->subCurve->xAxis->setTicker(ValTicker);
    ui->subCurve->xAxis->setRange(-100,_POINT_COUNT);
    ui->subCurve->yAxis->setRange(-100,600);

    m_Timer=new QTimer;
    m_Timer_addData=new QTimer;
    connect(m_Timer_addData,&QTimer::timeout,this,&MainWindow::slots_TimeOut);
    connect(m_Timer,&QTimer::timeout,this,&MainWindow::slots_CurveOffSet);
    connect(ui->subCurve,&SubCurve::sig_UpdateTrack,this,&MainWindow::slots_UpdateTrack);

    m_Timer->start(100);
    m_Timer_addData->start(100);
    ui->subCurve->clearGraphs();

    QGridLayout *G_layout=new QGridLayout;
    G_layout->setVerticalSpacing(3);
    G_layout->setContentsMargins(0,0,0,0);
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0;i<16;i++)
    {
        QLabel *labValue=new QLabel;
        labValue->setText(QString("Qlabel %1").arg(i+1));
        labValue->setAlignment(Qt::AlignCenter);
        labValue->setStyleSheet("QLabel{color:rgb(0, 122, 255);}");
        labValue->setFixedHeight(36);
        G_layout->addWidget(labValue,i/8,i%8);

        QColor color=QColor(qrand() % 256, qrand() % 256, qrand() % 256);
        int r,g,b;
        color.getRgb(&r,&g,&b);
        labValue->setStyleSheet(QString("QLabel{ color:rgb(%0,%1,%2);}").arg(r).arg(g).arg(b));
        m_labList.append(labValue);

        fun_addGraph(ui->subCurve,color);
    }

    QVBoxLayout *V_layout=new QVBoxLayout;
    V_layout->setContentsMargins(0,0,0,0);
    V_layout->addWidget(ui->subCurve);
    V_layout->addLayout(G_layout);

    ui->centralwidget->setLayout(V_layout);

    for(int i=0;i<_POINT_COUNT;i++){
        slots_TimeOut();
    }
}

MainWindow::~MainWindow()
{
    qDeleteAll(m_labList);
    delete ui;
}



void MainWindow::slots_TimeOut()
{
    double w = (3.14/100)*5;  //w为角速度 ，可以理解为波浪的密度，越大密度越大
    double A = 40;    //  A表示振幅,可以理解为水波的高度,越大高度越高
    static double x;
    x++;
    double waveY = (double)(A * sin(w * x ));// waveY随着x的值改变而改变，从而得到正弦曲线

    static double count=0;
    count+=1;
    int diffCount;
    double begValue;
    QCPRange xRand;
    for(int i=0;i<ui->subCurve->graphCount();i++)
    {
        int num = qrand()%(200-100)+100;//产生300-500范围的随机岁
        float data=0;//除以10获取 30.0到50.0的随机数
        data+=80*i+num;

        ui->subCurve->graph(i)->addData((double)count,waveY+80*i);
        //ui->subCurve->graph(i)->addData((double)count,data);
        diffCount=ui->subCurve->graph(i)->data()->size()-_POINT_COUNT;
        for(int del=0;del<diffCount;del++)
        {
            begValue=ui->subCurve->graph(i)->data()->constBegin()->mainKey();
            ui->subCurve->graph(i)->data()->remove(begValue);
        }
        xRand=ui->subCurve->xAxis->range();
    }
    if(xRand.upper<count){
        ui->subCurve->xAxis->moveRange(count-xRand.upper);
    }
}

void MainWindow::slots_CurveOffSet()
{
    ui->subCurve->replot(QCustomPlot::rpQueuedReplot);
}

void MainWindow::slots_UpdateTrack()
{
    for(int i=0;i<ui->subCurve->m_CoorPoints.count();i++){
        m_labList[i]->setText(QString::number(ui->subCurve->m_CoorPoints[i].Value,'f',2));
    }
}

void MainWindow::fun_intUi(SubCurve *Plot)
{
    connect(Plot->xAxis, SIGNAL(rangeChanged(QCPRange)), Plot->xAxis2, SLOT(setRange(QCPRange)));
     connect(Plot->yAxis, SIGNAL(rangeChanged(QCPRange)), Plot->yAxis2, SLOT(setRange(QCPRange)));
     connect(Plot->yAxis2, SIGNAL(rangeChanged(QCPRange)), Plot->yAxis, SLOT(setRange(QCPRange)));
     //设置缩放方式iRangeDrag可拖动 iRangeZoom可缩放 iSelectPlottables选中曲线
     Plot->setInteractions(QCP::iRangeZoom|QCP::iRangeDrag);
     //设置主背景图
     //Plot->setBackground(QPixmap(":/images/background_W.png",nullptr,Qt::OrderedDither));
     Plot->setBackground(QColor(0,0,0,255));
     Plot->axisRect()->setBackground(QColor(0x00,0xff,0x00,0x00));//这种网格背景黑色，透明颜色

     //设置XY坐标的网格样式
     Plot->yAxis->ticker()->setTickCount(3);//主刻度个数设置
     Plot->xAxis->ticker()->setTickCount(3);//主刻度个数设置
     Plot->xAxis->grid()->setPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格白色虚线
     Plot->xAxis->grid()->setSubGridPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格黄色色点线
     Plot->xAxis->grid()->setSubGridVisible(false);//显示x轴子网格线
     Plot->xAxis->grid()->setZeroLinePen(QPen(QColor(0, 57, 63)));//x轴0线颜色白

     Plot->yAxis->grid()->setPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格白色虚线
     Plot->yAxis->grid()->setSubGridPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格浅黑色点线
     Plot->yAxis->grid()->setSubGridVisible(true);//显示Y轴子网格线
     Plot->yAxis->grid()->setZeroLinePen(QPen(QColor(0, 57, 63)));//y轴0线颜色白色
     //设置轴线颜色
     QColor AxisColor(QColor(0, 57, 63));
     Plot->xAxis->setBasePen(QPen(AxisColor));
     Plot->yAxis->setBasePen(QPen(AxisColor));
     Plot->yAxis2->setBasePen(QPen(AxisColor));
     //刻度线长度和颜色
     QPen pen;
     pen.setColor(AxisColor);//主刻度红色
 //    pen.setWidth(2);//线宽2
     Plot->xAxis->setTickPen(pen);
 //    Plot->xAxis->setTickLengthIn(15);//主刻度向内延伸15
 //    Plot->xAxis->setTickLengthOut(0);//主刻度向外延伸0
     pen.setColor(AxisColor);//子刻度蓝色
     Plot->xAxis->setSubTickPen(pen);
 //    Plot->xAxis->setSubTickLengthIn(10);//子刻度向内延伸10
 //    Plot->xAxis->setSubTickLengthOut(0);//子刻度向外延伸0

     Plot->yAxis->setTickPen(pen);
     Plot->yAxis2->setTickPen(pen);
     Plot->yAxis->setSubTickPen(pen);
     Plot->yAxis2->setSubTickPen(pen);
     //刻度字体设置
     Plot->xAxis->setTickLabelFont(QFont("Microsoft YaHei", 12, 32));
     Plot->xAxis->setTickLabelColor(QColor(Qt::white));
     Plot->yAxis->setTickLabelFont(QFont("Microsoft YaHei", 10, 30));
     Plot->yAxis->setTickLabelColor(QColor(Qt::white));
     Plot->yAxis2->setTickLabelColor(QColor(Qt::white));
     //刻度数量
     //Plot->xAxis->ticker()->setTickOrigin(3.8);
     //Plot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
     //刻度值格式
     //Plot->xAxis->setNumberFormat("gbc");//g灵活的格式,b漂亮的指数形式，c乘号改成×
     //Plot->xAxis->setNumberPrecision(1);//精度1

     //改变起始原点
     //Plot->xAxis->setRange(-15,15);
     //Plot->xAxis->ticker()->setTickOrigin(1);//改变刻度原点为1

     //线性结尾装饰
     Plot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//x轴终点箭头图案
     //    Plot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//x轴起点圆点图案
     Plot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//Y轴终点箭头图案

     //轴位置偏移量
     //Plot->xAxis->setPadding(50);//填充50的空间
     //Plot->xAxis->setOffset(50);//偏移50
     //Plot->yAxis->setOffset(-50);//偏移50
     //Plot->yAxis->setPadding(-5);//填充50的空间
     //标题字体设置
     Plot->xAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
     Plot->xAxis->setLabelColor(QColor(Qt::white));
     //    Plot->xAxis->setLabel("X");
     Plot->yAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
     Plot->yAxis->setLabelColor(QColor(Qt::white));
}

void MainWindow::fun_addGraph(SubCurve *Plot, QColor &color)
{
       QPen pen;
       pen.setColor(color);
       pen.setStyle(Qt::SolidLine);
       pen.setWidth(1);
       Plot->addGraph();
       Plot->graph(Plot->graphCount()-1)->setPen(pen);
       Plot->graph(Plot->graphCount()-1)->setLineStyle(QCPGraph::lsLine);
}

